<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\ShopModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class MenusController extends Controller
{
    /*
     * 添加菜单
      id:菜单id，修改时必传。
      name:菜单名称
      is_show：是否显示 0、不显示 1、显示
      parentId：父id
      sort：显示顺序
      path：跳转地址
      code:编码
      alias:别名
      source:图标
      category 分类 1菜单 2按钮
    */
    public function act_menus(Request $request){
        $id=$request->input('id')? $request->input('id'):"";
        $name=$request->input('name')? $request->input('name'):"";
        $path=$request->input('path')? $request->input('path'):"";
        $code=$request->input('code')? $request->input('code'):"";
        $source=$request->input('source')? $request->input('source'):"";
        $parentId=$request->input('parentId') && $request->input('parentId')>0? $request->input('parentId'):0;
        $category=$request->input('category')? $request->input('category'):"";
        if($name==""){
            $arr=["code"=>0,"msg"=>"请传递菜单或按钮名称","data"=>array()];
            return $arr;
        }
        if($category==""){
            $arr=["code"=>0,"msg"=>"请传递类型","data"=>array()];
            return $arr;
        }
        if($category == 2 && $parentId == 0){
            $arr=["code"=>0,"msg"=>"请选择菜单","data"=>array()];
            return $arr;
        }

        $data = array();
        $data['name'] = $name;
        $data['path'] = $path;
        $data['code'] = $code;
        $data['source'] = $source;

        if($id){
            $mid = DB::table("jy_menus")->where("id",$id)->update($data);
        }else{
            //查询父级是否是顶级菜单
            if($parentId>0){
                $menusinfo=DB::table("jy_menus")->where("id",$parentId)->first();
                if(!$menusinfo){
                    $arr=["code"=>0,"msg"=>"请检查数据格式是否有误","data"=>array()];
                    return $arr;
                }
                $first=$menusinfo->parentId;
                if($category == 2 && $first == 0){
                    $arr=["code"=>0,"msg"=>"不能给顶级菜单添加按钮","data"=>array()];
                    return $arr;
                }
            }
            $data['parentId'] = $parentId;
            $data['category'] = $category;

            $mid = DB::table("jy_menus")->insertGetId($data);
        }

        if($mid>0){
            $arr=["code"=>200,"msg"=>"保存成功","data"=>array()];
        }else{
            $arr=["code"=>0,"msg"=>"保存失败，请检查数据格式是否正确。","data"=>array()];
        }
        return $arr;

    }

    /*
      获取菜单列表
      参数
      id:菜单id
      parentId:父id

    */
    public function get_menus(Request $request){
        $admin_id = 1;
        $uinfo=DB::table("jy_admin")->where("id",$admin_id)->where("status",1)->first();
        if(!$uinfo){
            $arr=["code"=>0,"msg"=>"帐号不存在","data"=>array()];
            return $arr;
        }

        $is_super=$uinfo->is_super;
        $menus_ids=array();
        if(!$is_super){
            $menus_ids = DB::table('jy_admin_role as r')
                        ->leftJoin('jy_admin as a','r.role_id','=','a.role_id')
                        ->where('a.id','=',$admin_id)->first();
        }

        $menus = DB::table('jy_menus')->where('category','=',1)->where('parentId','=',0);
        if($menus_ids) $menus =$menus->whereIn('id',$menus_ids->menu_ids);
        $menus =$menus->orderBy('sort')->get()->toArray();
        $data=array();
        foreach($menus as $k=>$row){
            $menu_sub = DB::table('jy_menus')->where('category','=',1)->where('parentId','=',$row->id);
            if($menus_ids) $menu_sub = $menu_sub->whereIn('id',$menus_ids->menu_ids);
            $menu_sub = $menu_sub->orderBy('sort')->get()->toArray();
            if($menu_sub)
                $row->children=$menu_sub;
            $data[]=$row;
        }
        if($menus){
            $arr=["code"=>200,"msg"=>"获取数据成功","data"=>$data];
        }else
            $arr=["code"=>0,"msg"=>"获取数据失败","data"=>array()];

        return $arr;
    }
    /*
        获取所有菜单和按钮
    */
    public function get_all_menus(Request $request){
        $admin_id = 1;
        $uinfo=DB::table("jy_admin")->where("id",$admin_id)->where("status",1)->first();
        if(!$uinfo){
            $arr=["code"=>0,"msg"=>"帐号不存在","data"=>array()];
            return $arr;
        }

        $menus=DB::table('jy_menus')->where('category','=',1)->where('parentId','=',0)->orderBy('sort')->get()->toArray();
        $data=array();
        foreach($menus as $row){
            $menu_sub=DB::table('jy_menus')->where('category','=',1)->where('parentId','=',$row->id)->get()->toArray();
            if($menu_sub){
                $row->children = $menu_sub;
                //获取按钮
                foreach($menu_sub as $k => $row_sub){
                    $button=DB::table('jy_menus')->where('category','=',2)->where('parentId','=',$row_sub->id)->get()->toArray();
                    if($button){
                        $row->children[$k]->children=$button;
                    }
                }
            }
            $data[]=$row;
        }

        if($data){
            $arr=["code"=>200,"msg"=>"获取数据成功","data"=>$data];
        }else
            $arr=["code"=>0,"msg"=>"获取数据失败","data"=>array()];

        return $arr;
    }
    /*
    获取单个菜单或按钮
    参数
    id:菜单按钮id
    */
    public function menus_details(Request $request){
        $id = $request->input('id');
        if($id==""){
            $arr=["code"=>0,"msg"=>"id不能为空","data"=>array()];
            return $arr;
        }
        $re = DB::table('jy_menus')->where('id','=',$id)->first();
        if($re){
            $arr=["code"=>200,"msg"=>"获取成功","data"=>$re];
        }else{
            $arr=["code"=>0,"msg"=>"获取失败","data"=>array()];

        }
        return $arr;


    }
    /*
     删除菜单
        参数 id
   */
    public function del_menus(Request $request){

        $id=$request->input("id");
        if($id==""){
            $arr=["code"=>0,"msg"=>"id不能为空","data"=>array()];
            return $arr;
        }
        //查询子菜单
        $menu=DB::table("jy_menus")->select('parentId')->where("id",$id)->first();
        $re2 = false;
        $re1 = false;
        $re = false;
        if($menu){
            //不是一级菜单
            if($menu->parentId > 0){
                $re2=DB::table('jy_menus')->where("parentId",$id)->delete();//删除二级或三级下层
                $re1=DB::table('jy_menus')->where("id",$id)->delete();

                if($re1>0 || $re2>0){
                    $arr=["code"=>200,"msg"=>"删除成功","data"=>array()];
                } else{
                    $arr=["code"=>0,"msg"=>"删除失败","data"=>array()];
                }
            }else if($menu->parentId == 0){//一级菜单
                //查询二级菜单
                $arr_id =array();
                $menu_sub=DB::table("jy_menus")->select('id')->where("parentId",$id)->get()->toArray();
                foreach ($menu_sub as $key => $value) {
                    $arr_id[] = $value->id;
                }
                $str_id =implode($arr_id, ',');
                if($str_id){
                    $re2=DB::table('jy_menus')->whereIn("parentId",[$str_id])->delete();//删除二级菜单下层
                    $re1=DB::table('jy_menus')->where("id",[$str_id])->delete();//删除二级菜单
                }

                $re=DB::table('jy_menus')->where("id",$id)->delete();

                if($re1>0 || $re2>0 || $re>0){
                    $arr=["code"=>200,"msg"=>"删除成功","data"=>array()];
                } else{
                    $arr=["code"=>0,"msg"=>"删除失败","data"=>array()];
                }
            }
        }else{
            $arr=["code"=>0,"msg"=>"删除失败，请刷新再试","data"=>array()];
        }

        return $arr;
    }
}

?>